package ai.people.netmon.warning.service;

import ai.people.netmon.framework.domain.warning.dto.AnalysisRequestContext;
import ai.people.netmon.framework.exception.enums.CommonEnum;
import ai.people.netmon.framework.model.response.Result;
import ai.people.netmon.framework.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * 预警分析任务抽象
 *
 * @author ligua
 * @date 2022/08/15
 */
@Slf4j
public abstract class WarningAnalysisTaskLink implements  WarningAnalysisTask{

    private WarningAnalysisTaskLink next;

    public WarningAnalysisTaskLink next() {
        return next;
    }

    public WarningAnalysisTaskLink appendNext(WarningAnalysisTaskLink next) {
        this.next = next;
        return this;
    }

    public <T> Result<List<T>> doAnalysis(AnalysisRequestContext<T> paramContext){
        Result<List<T>> listResult = null;
        try {
            if(!paramContext.getInterruptFlag()){
                listResult = this.doTask(paramContext);
            }else {
                return listResult;
            }
        }catch (Exception e){
            log.error("分析任务执行失败， 请求上下文：{}， 异常详情：{}", paramContext, e);
            return ResultUtil.error(CommonEnum.SERVER_ERROR);
        }
        WarningAnalysisTaskLink next = this.next();
        if(next != null){
            return next.doAnalysis(paramContext);
        }
        return listResult;
    }

}
